﻿using System.Windows;
using DSLPedProjectWizard.Messages;
using DSLPedProjectWizard.Views;
using Dsl.Ped.Common;
using GalaSoft.MvvmLight.Command;
using System.Collections.Generic;
using GalaSoft.MvvmLight.Messaging;

namespace DSLPedProjectWizard.ViewModels
{
    /// <summary>
    /// ViewModel of <see cref="MainWizardWindow"/>
    /// </summary>
    public class MainWizardWindowViewModel : ViewModelBase
    {
        #region Fields

        private RelayCommand nextCommand;

        private RelayCommand previousCommand;

        private int currentIndex = 0;

        private List<IWizardPage> pages = null;

        private SettingsWizardView settingsWizardPage = null;

        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="MainWizardWindowViewModel"/> class.
        /// </summary>
        /// <param name="introWizardPage">The intro wizard page.</param>
        /// <param name="settingsWizardPage">The settings wizard page.</param>
        /// <param name="messenger">The <see cref="Messenger"/> for the communication with a main wizard page</param>
        public MainWizardWindowViewModel(IntroWizardView introWizardPage, SettingsWizardView settingsWizardPage, Messenger messenger)
        {
            this.pages = new List<IWizardPage>() { introWizardPage, settingsWizardPage };
            this.settingsWizardPage = settingsWizardPage;
            this.currentIndex = 0;

            messenger.Register<CheckFinishMessage>(this, OnCanFinishMessage);
        }

        /// <summary>
        /// Gets the current page content
        /// </summary>
        public FrameworkElement PageContent
        {
            get
            {
                return pages[currentIndex].View;
            }
        }

        /// <summary>
        /// Gets the command executed when prev button is pressed 
        /// </summary>
        public RelayCommand PreviousCommand
        {
            get
            {
                return previousCommand ?? (previousCommand = new RelayCommand(OnPreviousCommand));
            }
        }

        /// <summary>
        /// Gets the command executed when the next button is pressed
        /// </summary>
        public RelayCommand NextCommand
        {
            get
            {
                return nextCommand ?? (nextCommand = new RelayCommand(OnNextCommand));
            }
        }

        /// <summary>
        /// Gets enability of the finish button
        /// </summary>
        public bool IsFinishEnabled
        {
            get
            {
                var lastPage = currentIndex == pages.Count - 1;
                var canFinish = pages[currentIndex].CanFinish;

                return lastPage && canFinish;
            }
        }

        /// <summary>
        /// Gets value indicating whether the previous button is visible
        /// </summary>
        public bool IsPreviousVisible
        {
            get
            {
                return currentIndex != 0;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the next button is visible
        /// </summary>
        public bool IsNextVisible
        {
            get
            {
                return currentIndex != pages.Count - 1;
            }
        }

        /// <summary>
        /// Gets the name of a language as set by user
        /// </summary>
        public string LanguageName
        {
            get
            {
                return this.settingsWizardPage.LanguageName;
            }
        }

        /// <summary>
        /// Gets the default xml namespace of a language as set by user
        /// </summary>
        public string LanguageXmlNamespace
        {
            get
            {
                return this.settingsWizardPage.LanguageXmlNamespace;
            }
        }

        /// <summary>
        /// Gets the description of a language as set by user
        /// </summary>
        public string LanguageDescription
        {
            get
            {
                return this.settingsWizardPage.LanguageDescription;
            }
        }

        /// <summary>
        /// Handler method of <see cref="PreviousCommand"/>
        /// </summary>
        private void OnPreviousCommand()
        {
            currentIndex--;
            RaisePropertyChanged(() => PageContent);
            RaisePropertyChanged(() => IsPreviousVisible);
            RaisePropertyChanged(() => IsNextVisible);
            RaisePropertyChanged(() => this.IsFinishEnabled);
        }

        /// <summary>
        /// Handler method of <see cref="NextCommand"/>
        /// </summary>
        private void OnNextCommand()
        {
            currentIndex++;
            RaisePropertyChanged(() => PageContent);
            RaisePropertyChanged(() => IsPreviousVisible);
            RaisePropertyChanged(() => IsNextVisible);
            RaisePropertyChanged(() => this.IsFinishEnabled);
        }

        /// <summary>
        /// Handler method of <see cref="CheckFinishMessage"/> receive
        /// </summary>
        /// <param name="canFinishMessage"></param>
        private void OnCanFinishMessage(CheckFinishMessage canFinishMessage)
        {
            RaisePropertyChanged(() => IsFinishEnabled);
        }
    }
}
